package cn.com.backstage.controller.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_circle;

import cn.com.api.service.ApiU_circleService;

import cn.com.util.*;

public class U_circleBaseController {

	private static Logger logger = Logger.getLogger(U_circleBaseController.class);

	@Autowired
	private ApiU_circleService apiU_circleService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_circle-list")
	public ModelAndView u_circle_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_circle_list.jsp");
		List<ApiU_circle> u_circles = apiU_circleService.findU_circles();
		mv.addObject("u_circles", u_circles);
		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_circle-add-pre")
	public ModelAndView u_circle_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_circle_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiU_circle u_circle = apiU_circleService.findU_circleById(Integer.valueOf(id));
			mv.addObject("u_circle", u_circle);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_circle-add")
	public void u_circle_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String create_uid = request.getParameter("create_uid");
		String circle_name = request.getParameter("circle_name");
		String circle_no = request.getParameter("circle_no");
		String circle_desc = request.getParameter("circle_desc");
		String max_people = request.getParameter("max_people");
		String circle_notice = request.getParameter("circle_notice");
		String is_need_confirm = request.getParameter("is_need_confirm");
		String type = request.getParameter("type");
		String is_show_name = request.getParameter("is_show_name");
		String circle_label = request.getParameter("circle_label");
		String longitude = request.getParameter("longitude");
		String latitude = request.getParameter("latitude");
		String is_delete = request.getParameter("is_delete");
		String is_against = request.getParameter("is_against");
		String create_time = request.getParameter("create_time");
		String cate_id = request.getParameter("cate_id");
		String cate_name = request.getParameter("cate_name");

		Map<String, Object> map = new HashMap<String, Object>();
		// String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiU_circle u_circle = new ApiU_circle();
			/*
			 * u_circle.setId(id); u_circle.setCreate_uid(create_uid);
			 * u_circle.setCircle_name(circle_name);
			 * u_circle.setCircle_no(circle_no);
			 * u_circle.setCircle_desc(circle_desc);
			 * u_circle.setMax_people(max_people);
			 * u_circle.setCircle_notice(circle_notice);
			 * u_circle.setIs_need_confirm(is_need_confirm);
			 * u_circle.setType(type); u_circle.setIs_show_name(is_show_name);
			 * u_circle.setCircle_label(circle_label);
			 * u_circle.setLongitude(longitude); u_circle.setLatitude(latitude);
			 * u_circle.setIs_delete(is_delete);
			 * u_circle.setIs_against(is_against);
			 * u_circle.setCreate_time(create_time);
			 * u_circle.setCate_id(cate_id); u_circle.setCate_name(cate_name);
			 */
			apiU_circleService.insertU_circle(u_circle);
		}
		// edit
		else {
			ApiU_circle u_circle = apiU_circleService.findU_circleById(Integer.valueOf(id));
			/*
			 * u_circle.setId(id); u_circle.setCreate_uid(create_uid);
			 * u_circle.setCircle_name(circle_name);
			 * u_circle.setCircle_no(circle_no);
			 * u_circle.setCircle_desc(circle_desc);
			 * u_circle.setMax_people(max_people);
			 * u_circle.setCircle_notice(circle_notice);
			 * u_circle.setIs_need_confirm(is_need_confirm);
			 * u_circle.setType(type); u_circle.setIs_show_name(is_show_name);
			 * u_circle.setCircle_label(circle_label);
			 * u_circle.setLongitude(longitude); u_circle.setLatitude(latitude);
			 * u_circle.setIs_delete(is_delete);
			 * u_circle.setIs_against(is_against);
			 * u_circle.setCreate_time(create_time);
			 * u_circle.setCate_id(cate_id); u_circle.setCate_name(cate_name);
			 */
			apiU_circleService.updateU_circle(u_circle);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_circle-delete")
	public void u_circle_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			ApiU_circle apiU_circle = apiU_circleService.findU_circleById(Integer.valueOf(id));
			apiU_circle.setIs_delete(1);
			apiU_circleService.updateU_circle(apiU_circle);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "删除失败");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
